我用python/C++调用ChatGPT自制了一个聊天机器人 | 您所在的位置:网站首页 › 聊天机器人api 免费 › 我用python/C++调用ChatGPT自制了一个聊天机器人 |
我正在参加「掘金·启航计划」 1 ChatGPT完整版2015年,OpenAI由马斯克、美国创业孵化器Y Combinator总裁阿尔特曼、全球在线支付平台PayPal联合创始人彼得·蒂尔等硅谷科技大亨创立,公司核心宗旨在于实现安全的通用人工智能(AGI),使其有益于人类。 2022年12 月 1 日,OpenAI的联合创始人山姆·奥特曼在推特上公布ChatGPT并邀请人们免费试用 ChatGPT可以与人类进行谈话般的交互,可以回答追问,连续性的问题,承认其回答中的错误,指出人类提问时的不正确前提,拒绝回答不适当的问题,其性能大大超乎人们对弱人工智能的想象。 在AI写代码、修Bug,甚至还想统治人类?快速体验ChatGPT中,我给出了体验完整版ChatGPT的教程,本文就基于ChatGPT API做一个小应用 2 Python/C++调用ChatGPTPython/C++如何调用ChatGPT呢,这个问题直接问ChatGPT就好 以Python为例,接下来按照ChatGPT自己说的步骤执行 2.1 获取API秘钥进入OpenAI API点击Creat new secret key 接着会获得一个秘钥,第一时间复制它 首先安装openai,建议在虚拟环境中进行,以免破坏工作环境,虚拟环境的配置请看Anaconda安装与Python虚拟环境配置保姆级图文教程(附速查字典) pip install openai -i https://pypi.tuna.tsinghua.edu.cn/simple 复制代码接着进行接口测试 import openai # Initialize the OpenAI API client openai.api_key = "YOUR_API_KEY" # Define your prompt prompt = "Hello, how are you today?" # Generate a response from ChatGPT response = openai.Completion.create( engine="text-davinci-002", prompt=prompt, max_tokens=1024, n=1, stop=None, temperature=0.5, ) # Print the response print(response["choices"][0]["text"]) 复制代码可以收到ChatGPT的回话说明测试通过 I'm doing well, thank you for asking. How about you? 复制代码 2.3 设计简单UI做一个简单的UI界面 import openai import os, sys from tkinter import * from tkinter.font import Font from tkinter.ttk import * class AppUI(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.master.title('My ChatGPT vx:yhd13950307060') self.master.geometry('900x500') self.createWidgets() def createWidgets(self): self.top = self.winfo_toplevel() self.style = Style() self.style.configure('Tftitle.TLabelframe', font=('黑体', 12)) self.style.configure('Tftitle.TLabelframe.Label', font=('黑体', 12)) self.ftitle = LabelFrame(self.top, text='', style='Tftitle.TLabelframe') self.ftitle.place(relx=0.008, rely=0.017, relwidth=0.982, relheight=0.998) self.stext = Text(self.ftitle, font=('黑体', 12), wrap=NONE, ) self.stext.place(relx=0.017, rely=0.036, relwidth=0.957, relheight=0.412) # 垂直滚动条 self.VScroll1 = Scrollbar(self.stext, orient='vertical') self.VScroll1.pack(side=RIGHT, fill=Y) self.VScroll1.config(command=self.stext.yview) self.stext.config(yscrollcommand=self.VScroll1.set) # 水平滚动条 self.stextxscroll = Scrollbar(self.stext, orient=HORIZONTAL) self.stextxscroll.pack(side=BOTTOM, fill=X) self.stextxscroll.config(command=self.stext.xview) self.stext.config(xscrollcommand=self.stextxscroll.set) self.totext = Text(self.ftitle, font=('黑体', 12), wrap=NONE) self.totext.place(relx=0.017, rely=0.552, relwidth=0.957, relheight=0.412) self.VScroll2 = Scrollbar(self.totext, orient='vertical') self.VScroll2.pack(side=RIGHT, fill=Y) # 将滚动条与文本框关联 self.VScroll2.config(command=self.totext.yview) self.totext.config(yscrollcommand=self.VScroll2.set) # 水平滚动条 self.totextxscroll = Scrollbar(self.totext, orient=HORIZONTAL) self.totextxscroll.pack(side=BOTTOM, fill=X) self.totextxscroll.config(command=self.totext.xview) self.totext.config(xscrollcommand=self.totextxscroll.set) menubar = Menu(self.top, tearoff=False) # 创建一个菜单 self.style.configure('Tcleartext.TButton', font=('黑体', 12)) self.cleartext = Button(self.ftitle, text='清空', command=self.cleartext_Cmd, style='Tcleartext.TButton') self.cleartext.place(relx=0.239, rely=0.463, relwidth=0.086, relheight=0.073) self.style.configure('Taddyh.TButton', font=('黑体', 12)) self.addyh = Button(self.ftitle, text='查询', command=self.addyh_Cmd, style='Taddyh.TButton') self.addyh.place(relx=0.512, rely=0.463, relwidth=0.2, relheight=0.073) class App(AppUI): def __init__(self, master=None): AppUI.__init__(self, master) def cleartext_Cmd(self, event=None): self.stext.delete(1.0, "end") self.totext.delete(1.0, "end") def addyh_Cmd(self, event=None): cookiestext = self.stext.get(1.0, "end") response = openai.Completion.create( engine="text-davinci-002", prompt=cookiestext, max_tokens=1024, n=1, temperature=0.5, ) answer = (response["choices"][0]["text"]).split(".") for i in answer: self.totext.insert(1.0, i) self.totext.update() if __name__ == "__main__": top = Tk() App(top).mainloop() 复制代码 3 聊天问答运行程序,开始问答 Q1:世界上最好的编程语言是什么?A1:不同的编程语言适合不同的任务,世界上没有最好的编程语言 可以看到回答得还是挺有水准的,大家可以进行进一步的测试~ 私信我加入百人AI技术交流社群 |
CopyRight 2018-2019 实验室设备网 版权所有 |